|
|
|
|
|
|
|
Notice that the exact SQL syntax for single-column foreign key indexes is |
|
|
|
|
|
|
|
|
CONSTRAINT IndexName REFERENCES Tablename(ColumnName) |
|
|
|
|
|
|
|
|
As long as the column name you are referencing defines the PRIMARY KEY of the referenced table, you can omit the (ColumnName) portion of the CONSTRAINT clause. However, it is good programming practice to include the column name for clarity. |
|
|
|
|
|
|
|
|
Use the SQL-VB5 editor window to load SQLVB04.SQV. Modify the script to match the code in Listing 13.13, save it, and run the script. Your screen should resemble Figure 13.15. |
|
|
|
|
|
|
|
|
Figure 13.15.
A foreign key constraint cascades the related tables on-screen. |
|
|
|
|
|
|
|
|
What you have defined here is a rule that tells the Microsoft Jet database engine that, any time a user enters data into the EmpsTable.JobTitle column, the engine should refer to the JobsTable.JobTitle column to make sure that the value entered in EmpsTable.JobTitle can be found in one of the rows of JobsTable.JobTitle. If not, return an error message to the user and do not save the record to the data table. All that is done without writing any input validation code at all! |
|
|
|
|
|
|
|
|
You can set up foreign key relations between any two columns in any two tables. They need not have the same column name, but they must have the same data type. For example, you can add a table to the SQLVB04.MDB database that holds information about job titles and pay grades. However, in this table the column that holds the job title is called JobName. Enter the script in Listing 13.14, save it, and execute it. See Figure 13.16 for a guide. |
|
|
|
|
|